package com.cup.bddm.impala.service;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.cup.bddm.impala.Constant;
import com.cup.bddm.impala.dao.UserDao;
import com.cup.bddm.impala.po.User;
import com.cup.bddm.impala.vo.UserVo;
import com.cup.bddm.util.MD5Util;

@Service
public class UserService {

	static Logger logger = Logger.getLogger(UserService.class);

	@Autowired
	private UserDao userDao;

	public UserVo getUserByIns(String account, String insIdCd, String pwd) {

		UserVo userVo = new UserVo();
		try {
			User user = userDao.getUserInfoByNameAndInsCd(account, insIdCd);
			if (user == null) {
				logger.info("username or insIdCd error");
				userVo.setState(Constant.NOT_EXIST);
				userVo.setUser(user);
			} else {

				if (user.getUser_pwd().equals(MD5Util.MD5(pwd))) {
					logger.info("log in success!");
					userVo.setState(Constant.SUCCESS);
					userVo.setUser(user);
				} else {
					logger.info("pwd error");
					userVo.setState(Constant.PWD_ERROR);
					userVo.setUser(null);
				}

			}
		} catch (RuntimeException e) {
			logger.error("internal error!");
			userVo.setState(Constant.INTER_ERROR);

		}

		return userVo;
	}

	public UserVo changePwd(String account, String insIdCd, String pwd, String newPwd) {
		UserVo userVo = new UserVo();
		try {
			User user = userDao.getUserInfoByNameAndInsCd(account, insIdCd);
			if (user == null) {
				logger.info("username or insIdCd error");

				userVo.setState(Constant.NOT_EXIST);
				return userVo;
			}

			if (!user.getUser_pwd().equals(MD5Util.MD5(pwd))) {
				// if (!user.getUserPwd().equals(pwd)) {
				logger.info("pwd error");

				userVo.setState(Constant.PWD_ERROR);
				return userVo;
			}

			userDao.changePwd(user.getUser_id(), MD5Util.MD5(newPwd));
			logger.info("change pwd success!");

			userVo.setState(Constant.SUCCESS);
			return userVo;

		} catch (RuntimeException e) {
			logger.error("internal error!");
			userVo.setState(Constant.INTER_ERROR);
			return userVo;

		}
	}

	public UserVo addUserInfo(User user) {
		logger.info("insert street info");
		UserVo userVo = new UserVo();

		try {
			String loginNm = user.getLogin_name();
			String insCd = user.getCup_branch_ins_id_cd();

			if (StringUtils.isEmpty(loginNm) || StringUtils.isEmpty(insCd)) {
				logger.info("username or insIdCd is emp");
				userVo.setState(Constant.PARA_ERROR);
				userVo.setUser(user);
			}
			User tmpUser = userDao.getUserInfoByNameAndInsCd(loginNm, insCd);
			if (tmpUser != null) {
				logger.info("username exists");
				userVo.setState(Constant.ALREADY_EXIST);
				userVo.setUser(user);
			} else {
				String pwd = user.getUser_pwd();
				user.setUser_pwd(MD5Util.MD5(pwd));

				if (userDao.addUserInfo(user)) {
					logger.info("add user info success");
					userVo.setState(Constant.SUCCESS);

				} else {
					logger.info("add user info fail!");
					userVo.setState(Constant.INTER_ERROR);
					userVo.setUser(user);
				}

			}

		} catch (RuntimeException e) {
			logger.error("internal sql error!", e);
			userVo.setState(Constant.INTER_ERROR);

		} catch (Exception e) {
			logger.error("internal error!", e);
			userVo.setState(Constant.INTER_ERROR);
		}
		return userVo;
	}

}
